#include <algorithm>
#include <iostream>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

class Solution {
public:
    vector<vector<string>> displayTable(vector<vector<string>>& orders) {
        unordered_set<string> food_set;
        for (vector<string>& item : orders) {
            food_set.insert(item[2]);
        }
        vector<string> food_array(food_set.begin(), food_set.end());
        sort(food_array.begin(), food_array.end());
        unordered_map<string, int> food_index_map;
        for (int i = 0; i < food_array.size(); i++) {
            food_index_map[food_array[i]] = i;
        }
        map<int, vector<int>> ans_map;
        for (vector<string>& item : orders) {
            int table = stoi(item[1]);
            if (ans_map.find(table) == ans_map.end()) {
                ans_map.insert({table, vector<int>(food_array.size())});
            }
            ans_map[table][food_index_map[item[2]]]++;
        }
        vector<vector<string>> ans;
        ans.reserve(ans_map.size() + 1);
        vector<string> column_name = {"Table"};
        column_name.insert(column_name.end(), food_array.begin(), food_array.end());
        ans.emplace_back(column_name);
        for (auto item : ans_map) {
            vector<string> tmp = {to_string(item.first)};
            tmp.reserve(item.second.size() + 1);
            for (auto it : item.second) {
                tmp.emplace_back(to_string(it));
            }
            ans.emplace_back(tmp);
        }
        return ans;
    }
};